package com.hrt.util;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

@Repository  
public class SessionInterceptor extends HandlerInterceptorAdapter {  

	   
	    @SuppressWarnings({ "rawtypes", "unchecked" })  
	    @Override  
	    public boolean preHandle(HttpServletRequest request,  
	            HttpServletResponse response, Object handler) throws Exception {  
	  
	        request.setCharacterEncoding("UTF-8");  
	        response.setCharacterEncoding("UTF-8");  
	        response.setContentType("text/html;charset=UTF-8");  
	  
	        // 后台session控制  
	        String[] noFilters = new String[] { "login.jsp",  
	                "index.jsp" };  
	        String uri = request.getRequestURI();  
	  
	        if (uri.indexOf("manage") != -1) {  
	            boolean beFilter = true;  
	            for (String s : noFilters) {  
	                if (uri.indexOf(s) != -1) {  
	                    beFilter = false;  
	                    break;  
	                }  
	            }  
	            if (beFilter) {  
	                Object obj = request.getSession().getAttribute(  
	                		WebParams.ON_LINE_MANAGER);  
	                if (null == obj) {  
	  
	                    // 未登录  
	                    PrintWriter out = response.getWriter();  
	                    StringBuilder builder = new StringBuilder();  
	                    builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");  
	                    builder.append("alert(\"页面过期，请重新登录\");");  
	                    builder.append("window.top.location.href=\"");  
	                    builder.append(WebParams.MANAGE_PATH);  
	                    builder.append("/login.jsp\";</script>");  
	                    out.print(builder.toString());  
	                    out.close();  
	                    return false;  
	                }
	                /*else {  
	                    // 添加日志  
	                    String operateContent = WebParams.operateContent(uri);  
	                    if (null != operateContent) {  
	                        String url = uri.substring(uri.indexOf("background"));  
	                        String ip = request.getRemoteAddr();  
	                        Integer userId = ((SystemUserForm) obj).getId();  
	                        SystemLoggerForm form = new SystemLoggerForm();  
	                        form.setUserId(userId);  
	                        form.setIp(ip);  
	                        form.setOperateContent(operateContent);  
	                        form.setUrl(url);  
	                        this.systemLoggerService.edit(form);  
	                    }  */
	                }  
	            }  
	      
	  
	       
	  
	        return super.preHandle(request, response, handler);  
	    }  
	  
}
